package view.panel.item
{
	import core.Control;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import global.Resource;
	
	import utils.DisplayTool;
	import utils.TextTool;
	
	import view.components.ImgButton;
	import view.components.NumericStepper;
	import view.components.list.ITileListItem;
	import view.panel.prompt.msg.ItemShortCutMsg;
	
	public class ItemOwn extends Sprite implements ITileListItem
	{
		private var _data:Object;
		private var _bg:DisplayObject;
		private var _imgBg:DisplayObject;
		private var title:TextField;
		private var itemImg:ItemImg;
		private var but:ImgButton;
		
		private var id:String;
		private var describ:TextField;
		
		private var numericStepper:NumericStepper;
		private var butPresent:ImgButton;
		
		public function ItemOwn()
		{
			initBg();
			initTitle();
			initItemImg();
			initBut();
			initNumericStepper();
		}
		
		private function initNumericStepper():void
		{
			numericStepper = new NumericStepper();
			numericStepper.y = 16;
			numericStepper.x = 130;
			numericStepper.width = 65;
			numericStepper.height = 20;
			addChild(numericStepper);
			numericStepper.visible = false;
		}
		
		private function initBut():void
		{
			but = new ImgButton(200,15,0,onBut,45,22,"butBg",null,"使 用");
			addChild(but);
			
			butPresent = new ImgButton(200,15,1,onBut,45,22,"butBg",null,"赠 送");
			addChild(butPresent)
			butPresent.visible = false;
		}
		
		private function onBut(event:MouseEvent):void
		{
			if (int(data.@count) <=0 )
			{
				var hintMsg:ItemShortCutMsg = new ItemShortCutMsg();
				Control.getInstance().alertYesNoPanelControl.open(hintMsg);
			}
			else
			{
				switch (event.target.id)
				{
					case 0:
						Control.getInstance().itemPanelControl.useItem(id);
						but.mouseEnabled = false;
						break;
					case 1:
						Control.getInstance().itemPanelControl.sendItem(id.toString(),numericStepper.value.toString());
						break;
				}
			}
			
		}
		
		private function initItemImg():void
		{
			itemImg = new ItemImg(_imgBg.width,_imgBg.height);
			itemImg.x = _imgBg.x;
			itemImg.y = _imgBg.y;
			addChild(itemImg);
		}
		
		private function initTitle():void
		{
			title = TextTool.getTextField(60,20,14,0xFFFFFF,false,true);
			title.x = _imgBg.x + _imgBg.width + 3;
			title.y = 15;
			addChild(title);
			
			describ = TextTool.getTextField(180,50,12,0xFFFFFF);
			describ.multiline = true;
			describ.wordWrap = true;
			describ.x = title.x;
			describ.y = 35;
			addChild(describ);
		}
		
		private function initBg():void
		{
			_bg = Resource.getShape("butBg2");
			_bg.width = 254;
			_bg.height = 84;
			addChild(_bg);
			
			_imgBg = Resource.getShape("itemBg");
			_imgBg.x = 15;
			_imgBg.y = 15;
			addChild(_imgBg);
			
			//itemImg = new RemoteImage(
		}
		
		public function get self():DisplayObject
		{
			return this;
		}
		
		public function update(data:Object):void
		{
			but.mouseEnabled = true;
			_data = data;
			
			id = data.@itemId;
			title.text = data.@name;
			describ.text = data.@desc;
			itemImg.updateCount(data.@count);
			
			if ( !itemImg.loaded)
			{
				itemImg.loadImg("resource/item/" + data.@itemId + ".png");
			}
			
			var canUse:int = int(data.@canUse);
			if (canUse == 0)
			{
				but.mouseEnabled = false;
			}
			
			if (itemImg.count <= 0)
			{
				//butPresent.mouseEnabled = false;
				numericStepper.mouseEnabled = false;
			}
			else
			{
				//butPresent.mouseEnabled = true;
				numericStepper.mouseEnabled = true;
				
				numericStepper.maximum = itemImg.count;
				numericStepper.minimun = 1;
			}
		}
		
		public function switchState(state:int):void
		{
			switch (state)
			{
				case 0:
					but.visible = true;
					butPresent.visible = false;
					numericStepper.visible = false;
					break;
				case 1:
					but.visible = false;
					butPresent.visible = true;
					numericStepper.visible = true;
					break;
			}
			
		}
		
		public function drawSelectBg():void
		{
		}
		
		public function drawOverBg():void
		{
			
		}
		
		public function drawOutBg():void
		{
			
		}
		
		public function get selected():Boolean
		{
			return false;
		}
		
		public function set selected(v:Boolean):void
		{
		}
		
		public function get data():Object
		{
			return _data;
		}
		
		public function destroy():void
		{
			numericStepper.destroy();
			numericStepper = null;
			_data = null;
			DisplayTool.removeAllChild(this);
			_bg = null;
			_imgBg = null;
			title = null;
			but.destroy();
			but = null;
			itemImg.destroy();
			itemImg = null;
		}
		
		public function getInstance():ITileListItem
		{
			return new ItemOwn();
		}
		
		private var _index:int = -1;
		
		public function get index():int
		{
			return _index;
		}
		
		public function set index(v:int):void
		{
			_index = v;
		}
		
		override public function get height() : Number
		{
			return _bg.height;
		}
		
	}
}